package com.slice.upload.base;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName StrategyCounter
 * @Description 策略计数器
 * @Author frinder
 * @Date 2019/9/11 15:58
 * @Verion 1.0
 */
public class StrategyCounter {

    /**
     * 计数器
     */
    private final static ThreadLocal<AtomicInteger> STRATEGY_COUNTER = new ThreadLocal<>();

    /**
     * 设置计数器
     */
    public static void set() {
        STRATEGY_COUNTER.set(new AtomicInteger(0));
    }

    /**
     * 获取当前计数器值
     *
     * @return
     */
    public static int getValue() {
        int value = STRATEGY_COUNTER.get().intValue();
        return value;
    }

    /**
     * 获取当前计数器值并++
     *
     * @return
     */
    public static int getAndIncrement() {
        return STRATEGY_COUNTER.get().getAndIncrement();
    }

    /**
     * 清除计数器
     */
    public static void clear() {
        STRATEGY_COUNTER.remove();
    }
}
